package com.Shop.Center.controller;

import com.Shop.Center.entity.MonthStatistical;
import com.Shop.Center.entity.YearStatistical;
import com.Shop.Center.service.AdminLoginService;
import com.Shop.Center.service.AdminUserService;
import com.Shop.Center.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class StatisticalController {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Resource
    private OrderService orderService;
    @Resource
    private AdminLoginService adminLoginService;

    @ResponseBody
    @PostMapping("/login_statistical")
    public Boolean runLogin(@RequestParam("name") String name, @RequestParam("password") String password){
        return adminLoginService.user_login(name, password);
    }
    //统计指定日期内的订单数量（未被删除的且支付状态为已支付的订单）
    @ResponseBody
    @PostMapping("statistical/specified")
    public int specifiedQuery(@RequestParam("startDate") String start,@RequestParam("endDate") String end) throws ParseException {
        if(!AdminLoginService.is_login) return -1;
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        if(startDate != null && endDate != null)
            if(startDate.compareTo(endDate) <= 0)   //startDate小于等于endDate则可进行查询
                return orderService.specifiedQuery(startDate,endDate);
        return 0;
    }

    //返回近12个月的订单数量（未被删除的且支付状态为已支付）
    @ResponseBody
    @GetMapping("statistical/year")
    public List<YearStatistical> yearOrder(){
        if(!AdminLoginService.is_login) return null;
        List<YearStatistical> list = orderService.yearOrder();
        System.out.println(list.get(0).toString());
        return list;
    }

    //返回近30天的订单数量（未被删除的且支付状态为已支付）
    @ResponseBody
    @GetMapping("statistical/month")
    public List<MonthStatistical> monthOrder(){
        if(!AdminLoginService.is_login) return null;
        return orderService.monthOrder();
    }
}
